﻿using System;
using System.Drawing;
using System.Runtime.Serialization;
using System.Web.UI.WebControls;
using ApplicationGenerator.BusinessModule.Elements;
using ApplicationGenerator.CommonModule;
using ApplicationGenerator.Forms.Interfaces;
using ApplicationGenerator.Forms.Utilities;
using ApplicationGenerator.BusinessModule.Interfaces;
using System.ComponentModel;

namespace ApplicationGenerator.Forms.WebControls
{
    /// <summary>
    /// Kontrolka pozwalająca na wyświetlanie wartości tekstowych
    /// </summary>
    [Serializable]
    [ToolboxBitmap(typeof(TextBox))]
    public class TextBoxControl : BaseMappedControl, IValidation, ILoadControl, ISaveControl, IInitializeControl
    {
        #region Fields
        /// <summary>
        /// Webowa kontrolka TextBox
        /// </summary>
        [NonSerialized]
        private TextBox _textBox;

        #endregion

        #region Properties
        /// <summary>
        /// Kontrolka webowa powiązana z daną kontrolką
        /// </summary>
        [Browsable(false)]
        public override WebControl Control
        {
            get
            {
                if (_textBox == null)
                {
                    _textBox = new TextBox();
                }

                return _textBox;
            }
        }

        /// <summary>
        /// Czy kontrolka ma być tylko do odczytu
        /// </summary>
        [Category("Rozszerzone dane")]
        [DisplayName("Tylko do odczytu")]
        [Description("Stwierdza, czy kontrolka ma być tylko do odczytu.")]
        public bool IsReadOnly
        {
            get;
            set;
        }

        #endregion

        #region ILoadControl Members
        /// <summary>
        /// Ładuje kontrolkę wartością z modelu
        /// </summary>
        /// <param name="modelElement">Model</param>
        public void Load(IModel modelElement)
        {
            if (modelElement == null || modelElement.ModelInstance == null)
            {
                return;
            }
            string[] pathParts = Path.Split(new char[] { '.' });
            _textBox.Text = TextFieldsConverter.ConvertToTextField(modelElement, pathParts[pathParts.Length - 1]);
        }

        #endregion

        #region ISaveControl Members
        /// <summary>
        /// Zapisuje aktualną wartość kontrolki do modelu
        /// </summary>
        /// <param name="modelElement">Model</param>
        void ISaveControl.Save(IModel modelElement)
        {
            if (modelElement != null && !IsReadOnly)
            {
                string[] pathParts = Path.Split(new char[] { '.' });
                modelElement.SetProperty(pathParts[pathParts.Length - 1], TextFieldsConverter.ConvertToValueField(modelElement, pathParts[pathParts.Length - 1], _textBox.Text));
            }
        }

        #endregion

        #region IInitializeControl Members
        /// <summary>
        /// Inicjalizuje kontrolkę
        /// </summary>
        public void Initialize()
        {
            _textBox.ReadOnly = IsReadOnly;
        }

        #endregion
    }
}
